var ctx=window.daijia.ctx;
var map = null; //地图对象

var offset=0;
var limit=20;
var pagingLimit=20;//显示分页时的条数
var currentTotalElements=0;//当前显示总条数


var pageLoading=true;//是否能滚动加载
var hasShowPages=false;//是否已显示分页
var firstLoadPages=true;//分页初始化时 不自动加载

//筛选条件
var search_orderNumber=null;
var search_clientPhone=null;
var search_driverPhone=null;
var search_companyName=null;
var search_startTime=null;
var search_createOrderName=null;
var search_endTime=null;
var search_startFinishTime=null;
var search_endFinishTime=null;
var search_orderType=[];
var search_status=[];
var search_warm=false;

$(document).ready(function(){
	
	$("#startTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#endTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	
	$("#startFinishTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#endFinishTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	
	initdata(false);
	
	//点击选择时间
	$("#dateRangeButton").click(function(){
		var dropdowncalendar=$(this).parent().find(".dropdown-calendar");
		if(dropdowncalendar.is(':hidden')){
			dropdowncalendar.show();
			if(dropdowncalendar.find(".datepicker").length==0){
				$('#dateRangeCalendar').DatePicker({
					flat: true,
					current: new Date(),
					date: [new Date()],
					calendars: 3,
					mode: 'range',
					starts: 1,
					onChange: function(formated, dates){
						$("#startTime").val(formated[0]);
						$("#endTime").val(formated[1]);
					}
				});
			}
		}
		else{
			dropdowncalendar.hide();
		}
	});
	
	//点击应用
	$("#applyIt").click(function(){
		$("#dateRangeText").text($("#startTime").val()+" 至 "+$("#endTime").val());
		$("#dateRangeButton").parent().find(".dropdown-calendar").hide();
		$("#searchButton").click();
	});
	
	//点击清空
	$("#resetIt").click(function(){
		$("#startTime").val("");
		$("#endTime").val("");
		$("#dateRangeText").text("选择时间");
		$("#dateRangeButton").parent().find(".dropdown-calendar").hide();
		$("#searchButton").click();
	});
	
	$(document).on("click",".daijia-paidan",function(){
		$("#paidanOrderId").val($(this).attr("data-id"));
		
		$("#paidanClientName").html("");
		$("#paidanClientPhone").html("");
		$("#paidanFromPlace").html("");		
		$("#dialog_paidan").modal();
	});
	
	$('#dialog_paidan').on('shown.bs.modal', function () {
		$.ajax({
	        url : ctx+'/order/getOrderInfo',
	        type:'GET',
	        async:false,
	        dataType : 'json',
	        data : {
	        	"orderId":$("#paidanOrderId").val()
	        },
	        success : function(order){
	        	$("#paidanOrderLng").val(order.clientLongitude);
	    		$("#paidanOrderLat").val(order.clientLatitude);
	    		
	    		showClientMaker(order.clientLongitude,order.clientLatitude);
	    		
	    		getDrivers(order.clientLongitude,order.clientLatitude);
	    		
	    		$("#paidanClientName").html(order.clientName);
	    		$("#paidanClientPhone").html(order.clientPhone);
	    		$("#paidanFromPlace").html(order.fromPlace);
	        }
		});
	});
	
	$('#distance').change(function(){
		showClientMaker($("#paidanOrderLng").val(),$("#paidanOrderLat").val());
		getDrivers($("#paidanOrderLng").val(),$("#paidanOrderLat").val());
	});
	
	$(document).on("click",".paigeita",function(){
		var driveraId=$(this).attr("data-id");
		if(window.confirm('确认要派单给该服务人员吗？')){
			$.post(ctx+'/order/diaoduPaidan',{'driverId':driveraId,'orderId':$("#paidanOrderId").val()},function(data){
				if(data.success){
					$.scojs_message("派单成功", $.scojs_message.TYPE_OK);
					$("#dialog_paidan").modal("hide");
					initdata(false);
				}
				else{
					$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
				}
			});
		}
	});
	
	//高级搜索
	$(".query-jiant").click(function(){
		var src=$("#jiant").attr("src");
		if(src.indexOf("jiant2")>=0){
			$("#jiant").attr("src",ctx+"/assets/img/jiant.png");
			$(".choose").slideDown(300);
		}else{
			$("#jiant").attr("src",ctx+"/assets/img/jiant2.png");
			$(".choose").slideUp(300);
		}
	});
	
	//销单
	$(document).on("click",".cancelthis",function(){
    	$("#dialog_cancel_id").val($(this).attr("data-id"));
    	$("input[name=cancel_reason]:checked").attr("checked",false);
    	$("#ok_modal_cancel").attr("disabled",false);
    	$('#dialog_cancel').modal();
    });
	$("#close_modal_cancel").click(function(){
		$('#dialog_cancel').modal("hide");
	});
	$("#ok_modal_cancel").click(function(){
		if($("input[name=cancel_reason]:checked").length==0){
			$.scojs_message("请选择销单原因", $.scojs_message.TYPE_ERROR);
			return false;
		}
		if($("input[name=cancel_reason]:checked").val()=="other" && $("#dialog_cancel_content").val().length==0){
			$.scojs_message("请输入销单原因", $.scojs_message.TYPE_ERROR);
			return false;
		}
		var content="";
		if($("input[name=cancel_reason]:checked").val()=="other"){
			content=$("#dialog_cancel_content").val();
		}
		else{
			content=$("input[name=cancel_reason]:checked").val();
		}
		$("#ok_modal_cancel").attr("disabled",true);
		$.post($("#cancel_form").attr("action"),{'id':$("#dialog_cancel_id").val(),'content':content},function(data){
            if(data.success){
            	$.scojs_message("销单成功", $.scojs_message.TYPE_OK);
            	$('#dialog_cancel').modal("hide");
				initdata(false);
            }
            else{
            	$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
            }
		},'json');

	});
	
	//强制销单
	$(document).on("click",".powercancelthis",function(){
		$("#dialog_powercancel_id").val($(this).attr("data-id"));
    	$("input[name=powercancel_reason]:checked").attr("checked",false);
    	$("#ok_modal_powercancel").attr("disabled",false);
    	$('#dialog_powercancel').modal();
    });
	$("#close_modal_powercancel").click(function(){
		$('#dialog_powercancel').modal("hide");
	});
	$("#ok_modal_powercancel").click(function(){
		if($("input[name=powercancel_reason]:checked").length==0){
			$.scojs_message("请选择强制销单原因", $.scojs_message.TYPE_ERROR);
			return false;
		}
		if($("input[name=powercancel_reason]:checked").val()=="other" && $("#dialog_powercancel_content").val().length==0){
			$.scojs_message("请输入强制销单原因", $.scojs_message.TYPE_ERROR);
			return false;
		}
		var content="";
		if($("input[name=powercancel_reason]:checked").val()=="other"){
			content=$("#dialog_powercancel_content").val();
		}
		else{
			content=$("input[name=powercancel_reason]:checked").val();
		}
		$("#ok_modal_powercancel").attr("disabled",true);
		$.post($("#powercancel_form").attr("action"),{'id':$("#dialog_powercancel_id").val(),'content':content},function(data){
            if(data.success){
            	$.scojs_message("销单成功", $.scojs_message.TYPE_OK);
            	$('#dialog_powercancel').modal("hide");
				initdata(false);
            }
            else{
            	$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
            }
		},'json');

	});
	
	//收回
	$(document).on("click",".revokethis",function(){
    	$("#dialog_revoke_id").val($(this).attr("data-id"));
    	$("#dialog_revoke_content").val("");
    	$("#ok_modal_revoke").attr("disabled",false);
    	$('#dialog_revoke').modal();
    });
	$("#close_modal_revoke").click(function(){
		$('#dialog_revoke').modal("hide");
	});
	$("#ok_modal_revoke").click(function(){
		var content=$("#dialog_revoke_content").val();
		if(content.length==0){
			$.scojs_message("请输入备注", $.scojs_message.TYPE_ERROR);
			return false;
		}
		$("#ok_modal_revoke").attr("disabled",true);
		$.post($("#revoke_form").attr("action"),{'id':$("#dialog_revoke_id").val(),'content':content},function(data){
            if(data.success){
            	$.scojs_message("收回成功", $.scojs_message.TYPE_OK);
            	$('#dialog_revoke').modal("hide");
				initdata(false);
            }
            else{
            	$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
            }
		},'json');

	});
	
	//强制收回
	$(document).on("click",".powerrevokethis",function(){
    	$("#dialog_powerrevoke_id").val($(this).attr("data-id"));
    	$("#dialog_powerrevoke_content").val("");
    	$("#ok_modal_powerrevoke").attr("disabled",false);
    	$('#dialog_powerrevoke').modal();
    });
	$("#close_modal_powerrevoke").click(function(){
		$('#dialog_powerrevoke').modal("hide");
	});
	$("#ok_modal_powerrevoke").click(function(){
		var content=$("#dialog_powerrevoke_content").val();
		if(content.length==0){
			$.scojs_message("请输入备注", $.scojs_message.TYPE_ERROR);
			return false;
		}
		$("#ok_modal_powerrevoke").attr("disabled",true);
		$.post($("#powerrevoke_form").attr("action"),{'id':$("#dialog_powerrevoke_id").val(),'content':content},function(data){
            if(data.success){
            	$.scojs_message("收回成功", $.scojs_message.TYPE_OK);
            	$('#dialog_powerrevoke').modal("hide");
				initdata(false);
            }
            else{
            	$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
            }
		},'json');

	});
	
	//备注
	$(document).on("click",".memothis",function(){
		$("#dialog_memo_id").val($(this).attr("data-id"));
		$("#dialog_memo_newcontent").val("");
		$("#dialog_memo_oldcontent").html($(this).attr("data-value-memo"));
    	$("#ok_modal_memo").attr("disabled",false);
    	$('#dialog_memo').modal();
    });
	$("#close_modal_memo").click(function(){
		$('#dialog_memo').modal("hide");
	});
	$("#ok_modal_memo").click(function(){
		var content=$("#dialog_memo_newcontent").val();
		if(content.length==0){
			$.scojs_message("请输入备注", $.scojs_message.TYPE_ERROR);
			return false;
		}
		$("#ok_modal_memo").attr("disabled",true);
		$.post($("#memo_form").attr("action"),{'id':$("#dialog_memo_id").val(),'memo':content},function(data){
            if(data.success){
            	$.scojs_message("备注成功", $.scojs_message.TYPE_OK);
            	$('#dialog_memo').modal("hide");
				initdata(false);
            }
            else{
            	$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
            }
		},'json');

	});
	
	//追加
	$(document).on("click",".zhuijia",function(){
		window.open(ctx+'/order/addmore/'+$(this).attr("data-id"), '_blank', 'height=770,width=1220,scrollbars=yes,resizable=yes,alwaysRaised=yes,toolbar=no');
	});
	
	//完工
	$(document).on("click",".wangong",function(){
		window.open(ctx+'/order/complete/'+$(this).attr("data-id"), '_blank', 'height=500,width=900,scrollbars=yes,resizable=yes,alwaysRaised=yes,toolbar=no');
	});
	
	//回访
	$(document).on("click",".visitthis",function(){
		window.open(ctx+'/phonevisit/add/'+$(this).attr("data-id"), '_blank', 'height=650,width=1024,scrollbars=yes,resizable=yes,alwaysRaised=yes,toolbar=no');
	});
	
	//处理异常订单
	$(document).on("click",".handlewarm",function(){
		var thisButton=$(this);
		var orderId=$(this).attr("data-id");
		var flag = window.confirm("确认要处理该异常订单吗？");
		if(flag){
			$.post(ctx+"/order/handelWarmingOrder",{"orderId":orderId},function(result){
				$.scojs_message("处理成功", $.scojs_message.TYPE_OK);
				initdata(false);
			});
		}else{
		
		}
	});
	//查询
	$("#searchButton").click(function(){
		
		var DATE_FORMAT = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1} [0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/;
		if($("#startTime").val()!=""){
			if(!DATE_FORMAT.test($("#startTime").val())){
			   $.scojs_message("您输入的启始日期格式有误，正确格式如：2012-01-01 08:30", $.scojs_message.TYPE_ERROR);
			   return false;
			}
		}
		
		if($("#endTime").val()!=""){
			if(!DATE_FORMAT.test($("#endTime").val())){
				$.scojs_message("您输入的结束日期格式有误，正确格式如：2012-01-01 08:30", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		if($("#startTime").val()!="" && $("#endTime").val()!=""){
			if($("#startTime").val()>$("#endTime").val()){
				 $.scojs_message("启始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
				 return false;
			}
		}
		
		search_orderNumber=$("#orderNumber").val();
		search_clientPhone=$("#clientPhone").val();
		search_driverPhone=$("#driverPhone").val();
		search_companyName=$("#companyName").val();
		search_startTime=$("#startTime").val();
		search_endTime=$("#endTime").val();
		search_startFinishTime=$("#startFinishTime").val();
		search_endFinishTime=$("#endFinishTime").val();
		search_createOrderName=$("#createOrderName").val();
		var orderTypeArr=[];
		$("input[name=orderType]:checked").each(function(index,element){
			orderTypeArr.push($(element).val());
		});
		if(null!=orderTypeArr && orderTypeArr.length>0){
			search_orderType=orderTypeArr.join(",");
		}
		else{
			search_orderType=[];
		}
		
		
		var statusArr=[];
		$("input[name=status]:checked").each(function(index,element){
			statusArr.push($(element).val());
		});
		if(null!=statusArr && statusArr.length>0){
			search_status=statusArr.join(",");
		}
		else{
			search_status=[];
		}
		
		search_warm=$("input[name=warm]").is(':checked');
		
		offset=0;
		initdata(false);
		
	});
	
	$(document).on("keydown","body",function(){
		if(window.event.keyCode == 13){
			$("#searchButton").click();
		}
	});
	
	//滚动
//	$(window).scroll(function () {
//        //$(window).scrollTop()这个方法是当前滚动条滚动的距离
//        //$(window).height()获取当前窗体的高度
//        //$(document).height()获取当前文档的高度
//        var bot = 0; //bot是底部距离的高度
//        if ((bot + $(window).scrollTop()) >= ($(document).height() - $(window).height())) {
//           //当底部基本距离+滚动的高度〉=文档的高度-窗体的高度时；
//        	if(pageLoading){
//        		pageLoading=false;
//        		offset+=limit;
//        		initdata(true);
//        	}
//        }
//    });
	//开始导出
	$("#export_button").click(function(){
		
		window.location=ctx+"/order/exportOrder?"+"&orderNumber="+$("#orderNumber").val()+
		                "&clientPhone="+$("#clientPhone").val()+"&driverPhone="+$("#driverPhone").val()+
		                "&companyName="+$("#companyName").val()+"&orderType="+search_orderType+"&status="+search_status+
		                "&createOrderName="+$("#createOrderName").val()+
		                "&warm="+search_warm+"&startTime="+$("#startTime").val()+"&endTime="+$("#endTime").val()+
		                "&startFinishTime="+$("#startFinishTime").val()+"&endFinishTime="+$("#endFinishTime").val();
	});
	
	
	
});


function initdata(isappend){
	
//	showLoading();
	
	$.ajax({
        url : ctx+'/order/daijialistData',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"orderNumber":search_orderNumber,
        	"clientPhone":search_clientPhone,
        	"driverPhone":search_driverPhone,
        	"companyName":search_companyName,
        	"startTime":search_startTime,
        	"endTime":search_endTime,
        	"startFinishTime":search_startFinishTime,
        	"endFinishTime":search_endFinishTime,
        	"createOrderName":search_createOrderName,
        	"orderType":search_orderType,
        	"status":search_status,
        	"warm":search_warm,
        	"pager.offset":offset,
        	"limit":limit,
        },
        success : function(result){
        	var newcontent = '';
        	var totalElements=result.totalElements;
        	$("#NoData").text("");
        	
        	if(totalElements>0){
        		$(result.content).each(function(index,data){
        			var mainButton="";
        			var selectButton="";
        			var stauts="";
        			
            		if(data.status==0){
            			if(hasPermission_dispatch){
            				mainButton="<a href='javascript:;' data-id='"+data.id+"' class='btn btn-primary btn-xs daijia-paidan'>派单</a>";
            			}
            			else{
            				mainButton="<a href='"+ctx+"/order/detail/"+data.id+"' class='btn btn-primary btn-xs'>查看</a>";
            			}
            			if(hasPermission_powercancel){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link cancelthis'>销单</a></li>";
            			}
            			if(hasPermission_ordercreate){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link zhuijia'>追加</a></li>";
            			}
            			status="<span class='btn btn-xindan btn-xs'>未派单</span>";
            		}
            		else if(data.status==1 || data.status==7){
            			mainButton="<a href='"+ctx+"/order/detail/"+data.id+"' class='btn btn-primary btn-xs'>查看</a>";
            			if(hasPermission_powercancel){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link cancelthis'>销单</a></li>";
            			}
            			if(hasPermission_powerrevoke){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link revokethis'>收回</a></li>";
            			}
            			if(hasPermission_orderComplete){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link wangong'>完工</a></li>";
            			}
            			if(hasPermission_ordercreate){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link zhuijia'>追加</a></li>";
            			}
            			status="<span class='btn btn-warning btn-xs'>已派单</span>";
            		}
            		else if(data.status==2){
            			mainButton="<a href='"+ctx+"/order/detail/"+data.id+"' class='btn btn-primary btn-xs'>查看</a>";
            			
            			if(hasPermission_powercancel){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link powercancelthis'>销单</a></li>";
            			}
            			if(hasPermission_powerrevoke){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link powerrevokethis'>收回</a></li>";
            			}
            			if(hasPermission_orderComplete){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link wangong'>完工</a></li>";
            			}
            			if(hasPermission_ordercreate){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link zhuijia'>追加</a></li>";
            			}
            			status="<span class='btn btn-danger btn-xs'>已接单</span>";
            		}
            		else if(data.status==3 || data.status==6){
            			if(data.warm){
            				mainButton="<a href='Javascript:;' data-id='"+data.id+"' class='btn btn-primary btn-xs handlewarm'>处理</a>";
            			}
            			else{
            				mainButton="<a href='"+ctx+"/order/detail/"+data.id+"' class='btn btn-primary btn-xs'>查看</a>";
            			}
            			if(hasPermission_ordercreate){
            				selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link zhuijia'>追加</a></li>";
            			}
            			status="<span class='btn btn-success btn-xs'>已完成</span>";
            			
            		}
            		else if(data.status==5){
            			if(hasPermission_ordercreate){
            				mainButton="<a href='javascript:;' data-id='"+data.id+"' class='btn btn-primary btn-xs zhuijia'>追加</a>";
            			}
            			else{
            				mainButton="<a href='"+ctx+"/order/detail/"+data.id+"' class='btn btn-primary btn-xs'>查看</a>";
            			}
            			status="<span class='btn btn-default btn-xs'>已销单</span>";
            		}
            		
            		selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' data-value-memo='"+data.memo+"' class='btn-link memothis'>备注</a></li>";
        			
            		if(hasPermission_phonevisit){
            			if(data.status==3 || data.status==5 || data.status==6){
            				var visitStr="回访";
                			if(data.hasVisited){
                				visitStr="已回访";
                			}
                			selectButton+="<li><a href='javascript:;' data-id='"+data.id+"' class='btn-link visitthis'>"+visitStr+"</a></li>";
            			}
            		}
            		
            		var allButton="<div class='btn-group'>"+
            							mainButton+
									  "<button type='button' class='btn "+"btn-primary"+" btn-xs dropdown-toggle' data-toggle='dropdown' aria-expanded='false'>"+
									    "<span class='caret'></span>"+
									    "<span class='sr-only'>Toggle Dropdown</span>"+
									  "</button>"+
									  "<ul class='dropdown-menu' role='menu'>"+
									  	selectButton+
									  "</ul>"+
									"</div>";
            		
            		//异常订单
            		var warmSpan="";
            		if(data.warm){
            			warmSpan="<span class='label label-danger status-td'>异</span>";
            		}
            		var couponSpan="";
            		if(data.couponId!=null){
            			couponSpan="<span class='label status-td' style='color:#D4DDF1;background-color:#0063C3;'>券</span>";
            		}
            		
            		//保险
            		var insurance="";
            		if(data.insurance){
            			insurance="<span class='label label-success status-td baoxian' title='"+data.insurance+"'>保</span>";
            		}
            		
            		//所属公司
            		var companyNameTd="";
            		if(mainCompany){
            			companyNameTd="<td>"+data.companyAbbreviation+"</td>";
            		}
            		
            		//结算方式
            		var realCashTD="<td>-</td>";
            		if((data.status==3 || data.status==6) && null!=data.preAndAfterPayType){
            			realCashTD="<td>"+data.realCash+"元（"+data.preAndAfterPayType+"）</td>";
            		}
            		//加收费用
            		var guoluMoney;
            		var yuanchengMoney;
            		var otherMoney;
            		if(data.guoluMoney>0){
            			guoluMoney="<font color='red'>"+data.guoluMoney+"</font>元";
            		}else{
            			guoluMoney=data.guoluMoney+"元";
            		}
            		if(data.yuanchengMoney>0){
            			yuanchengMoney="<font color='red'>"+data.yuanchengMoney+"</font>元";
            		}else{
            			yuanchengMoney=data.yuanchengMoney+"元";
            		}
            		if(data.otherMoney>0){
            			otherMoney="<font color='red'>"+data.otherMoney+"</font>元";
            		}else{
            			otherMoney=data.otherMoney+"元";
            		}
            		var addMoney="<td>-</td>";
            		if((data.status==3 || data.status==6) && null!=data.preAndAfterPayType){
            			addMoney="<td>(过路费："+guoluMoney+"，远程费："+yuanchengMoney+"，其他费："+otherMoney+")</td>";
            		}
            		
            		var tiaojiaTd="";
            		if(tiaojia){
            			tiaojiaTd="<td>"+(data.justKilometres==null?"-，":+data.justKilometres+"公里，")+(data.justMoney==null?"-":+data.justMoney+"元")+"</td>";
            		}
            		
            		//备注
            		var memo=data.memo;
            		if(memo.length>27){
            			memo=memo.substring(0,27)+"...";
            		}
            		
            		//销单原因
            		var cancelContent=data.cancelContent;
            		if(cancelContent.length>20){
            			cancelContent=cancelContent.substring(0,20)+"...";
            		}
            		
            		var toPlace=data.toPlace;
            		if(toPlace.length>15){
            			toPlace=toPlace.substring(0,15)+"...";
            		}
            		
            		//代驾地点详细地址
            		var fromPlaceDetail=data.fromPlaceDetail;
            		if(fromPlaceDetail!=null && fromPlaceDetail.length>0){
	            		if(fromPlaceDetail.length>14){
	            			fromPlaceDetail="("+fromPlaceDetail.substring(0,14)+"...)";
	            		}else{
	            			fromPlaceDetail="("+fromPlaceDetail+")";
	            		}
            		}else{
            			fromPlaceDetail="";
            		}
            		//目的地详细地址
            		var toPlaceDetail=data.toPlaceDetail;
            		if(toPlaceDetail!=null && toPlaceDetail.length>0){
	            		if(toPlaceDetail.length>14){
	            			toPlaceDetail="("+toPlaceDetail.substring(0,14)+"...)";
	            		}else{
	            			toPlaceDetail="("+toPlaceDetail+")";
	            		}
            		}else{
            			toPlaceDetail="";
            		}
	    newcontent+="<tr class='grid-data tr-hui' style='display:none;'>"+
						"<td>"+allButton+"</td>"+
						"<td>"+status+"</td>"+
						"<td><a href='"+ctx+"/order/detail/"+data.id+"' class='btn-link'>***"+data.orderNumber.substring(8,data.orderNumber.length)+"</a>"+couponSpan+warmSpan+insurance+"</td>"+
						"<td>"+data.serverTime+"</td>"+
						"<td>"+data.fromPlace+fromPlaceDetail+"</td>"+
						"<td>"+data.clientName+"</td>"+
						"<td>"+data.clientPhone+"</td>"+
						"<td>"+data.userPhone+"</td>"+
						"<td>"+toPlace+toPlaceDetail+"</td>"+
						realCashTD+
						"<td>"+data.shouldCash+"元</td>"+
						addMoney+
						tiaojiaTd+
						"<td>"+data.driverRealName+"</td>"+
						"<td>"+data.driverPhone+"</td>"+
						companyNameTd+
						"<td>"+data.orderType+"</td>"+
						"<td>"+(data.createOrderName==null?"":data.createOrderName)+"</td>"+
						"<td>"+data.finishTime+"</td>"+
						"<td>"+data.mileage+"公里</td>"+
						"<td>"+data.waitedTime+"分钟</td>"+
						"<td>"+memo+"</td>"+
						"<td>"+cancelContent+"</td>"+
						"<td>"+allButton+"</td>"+
					"</tr>";
        		});
        	}else{
              	 $("#NoData").text("当前页面暂无记录 ");
            }

        	if(!isappend){
        		$(".table tbody .grid-data").remove();
        	}
        	$(".table tbody").append(newcontent);
        	
        	$(".table tbody .grid-data").each(function(index,element){
        		$(element).fadeIn(200+(index*35));
        	});
        	
        	if(!hasShowPages){
        		if(!isappend){
            		currentTotalElements=result.content.length;
            		pageLoading=true;
            	}
        		else{
            		if(currentTotalElements<totalElements){
            			currentTotalElements+=result.content.length;
            			pageLoading=true;
            		}
            		else{
            			pageLoading=false;
            		}
            	}
        	}

        	if(hasShowPages){
    			firstLoadPages=true;
        		$(".pageshow li a").html("共"+totalElements+" 条数据");
        		$(".pagedata").paging(totalElements, {
        			format: '[< ncnnn >]',
        			perpage: pagingLimit,
        			page:(offset/limit)+1,
        			onSelect: function (page) {
        				limit=pagingLimit;
        				offset=(page-1)*limit;
        				if(!firstLoadPages){
        					initdata(false);
        				}
        				firstLoadPages=false;
        		    	return false;
        			},
        			onFormat: function (type) {
        				switch (type) {
        				case 'block': // n and c
        					if (!this.active)
        						return '';
        					else if (this.value != this.page)
        						return '<li><a href="#">' + this.value + '</a></li>';
        					return '<li class="active"><a href="#">' + this.value + '</a></li>';
        				case 'next': // >
        					return '<li><a href="#">&rsaquo;</a><li>';
        				case 'prev': // <
        						return '<li><a href="#">&lsaquo;</a><li>';
        				case 'first': // [
        						return '<li><a href="#">&laquo;</a><li>';
        				case 'last': // ]
        						return '<li><a href="#">&raquo;</a></li>';
        				}
        			}
        		});
        		
        	}
        	else{
        		//滚动达到pagingLimit条开始显示分页
//        		if(currentTotalElements==pagingLimit){
            		
            		pageLoading=false;
            		hasShowPages=true;
            		
            		$(".pageshow").show();
            		$(".pagedata").show();
            		$(".pageshow li a").html("共"+totalElements+" 条数据");
            		$(".pagedata").paging(totalElements, {
            			format: '[< ncnnn >]',
            			perpage: pagingLimit,
            			onSelect: function (page) {
            				limit=pagingLimit;
            				offset=(page-1)*limit;
            				if(!firstLoadPages){
            					initdata(false);
            				}
            				firstLoadPages=false;
            		    	return false;
            			},
            			onFormat: function (type) {
            				switch (type) {
            				case 'block': // n and c
            					if (!this.active)
            						return '';
            					else if (this.value != this.page)
            						return '<li><a href="#">' + this.value + '</a></li>';
            					return '<li class="active"><a href="#">' + this.value + '</a></li>';
            				case 'next': // >
            					return '<li><a href="#">&rsaquo;</a><li>';
            				case 'prev': // <
            						return '<li><a href="#">&lsaquo;</a><li>';
            				case 'first': // [
            						return '<li><a href="#">&laquo;</a><li>';
            				case 'last': // ]
            						return '<li><a href="#">&raquo;</a></li>';
            				}
            			}
            		});
            		
//            	}
        	}
        	
        }
	});
	
}


function showClientMaker(lng,lat){
	var point=new BMap.Point(lng,lat);
	map = new BMap.Map("allmap");  
	map.centerAndZoom(point, 13);
	map.enableScrollWheelZoom();

	var marker = new BMap.Marker(point);  // 创建标注
	map.addOverlay(marker);
	
	map.addControl(new BMap.MapTypeControl({anchor: BMAP_ANCHOR_TOP_RIGHT, mapTypes: [BMAP_NORMAL_MAP,BMAP_HYBRID_MAP]}));
    
    map.addControl(new BMap.NavigationControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}));  //右上角，仅包含平移和缩放按钮
    
    var ctrl = new BMapLib.TrafficControl({
       showPanel: false //是否显示路况提示面板
    });
    map.addControl(ctrl);
    ctrl.setAnchor(BMAP_ANCHOR_TOP_LEFT);
}

function getDrivers(lng,lat){
	var distance=$("#distance").val();
	$.get(ctx+'/driver/daijiaListDrivers',{'lng':lng,'lat':lat,'distance':distance},function(data){
		if(data && data.length>0){
			var maxdistance=0;
			$("#paidanDrivers").html('');
			$(data).each(function(i,driver){
				
				var detailStatus='';
				if(!driver.workCar){
					detailStatus='kongxian';
				}
				else{
					detailStatus='workcar';
				}
					
				var htm='<div id="driver_marker_'+driver.id+'" class="sj-mark driver_marker_class" style="position:absolute;z-index:9;">'+
							'<div class="sj-mark-ma sjcolor-'+detailStatus+'"><span>'+driver.realname+'</span></div>'+
							'<div class="sj-mark-rk rkcolor-'+detailStatus+'"></div>'+
						'</div>';
				
				if(driver.workCar){
					htm='<div id="driver_marker_'+driver.id+'" class="driver_workcar_marker_class" data-z-index="9998" style="position:absolute;z-index:9998;width:110px;">'+
				        	'<div class="em-'+detailStatus+'-l"></div>'+
				    		'<div class="em-'+detailStatus+'-con">'+driver.realname+'</div>'+
				            '<div class="em-'+detailStatus+'-r"></div>'+
				        '</div>';
				}
					
			    var driverpoint = new BMap.Point(driver.longitude,driver.latitude);
				var drivermarker = new BMapLib.RichMarker(htm, driverpoint, {"anchor": new BMap.Size(-62, -40), "enableDragging": true});
				drivermarker.driverid=driver.id;
				drivermarker.driverstatus=driver.status;
				
				map.addOverlay(drivermarker);
				
				$("#paidanDrivers").append("<div class='modal-sj-bd' style='display:none;'><button type='button' class='paigeita' data-id='"+driver.id+"'>派给</button><span>"+driver.realname+"</span><span>"+driver.phone+"</span><span style='font-size:12px;'>"+driver.distanceStr+"</span></div>");
			
				var currentdistance=map.getDistance(driverpoint,new BMap.Point(lng,lat));
				if(currentdistance>maxdistance){
					maxdistance=currentdistance;
				}
				
			});
			
			$(".paigeita").each(function(index,element){
        		$(element).parent().fadeIn((index+1)*300);
        	});
			
			
			maxdistance=(maxdistance/1000);
			if(maxdistance<=7){
				var zoom=13;
			}
			else if(maxdistance<=15){
				var zoom=12;	
			}
			else if(maxdistance<=22){
				var zoom=11;
			}
			map.centerAndZoom(new BMap.Point(lng,lat), zoom);
		}
		else{
			$("#paidanDrivers").html(distance+"公里范围内暂无推荐服务人员，您可以扩大范围看看。");
		}
	});
	
}

function showLoading(){
	if($('#loadblacklayer').length==0){
		var dw = $(".table").width();
		var dh = $(".table").height();
		var backlayer ="<div id='loadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
		$(".table tbody").append(backlayer);
		$('#loadblacklayer').css({"z-index":"9","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
	}
}

function hideLoading(){
	if($('#loadblacklayer').length>0){
		$('#loadblacklayer').remove();
	}
}

